import os
import cv2

# 定义函数进行四倍下采样并保存图片
def downsample_images(folder_list):
    for folder_path in folder_list:
        print(folder_path)
        # 检查文件夹是否存在
        if not os.path.exists(folder_path):
            print(f"文件夹 '{folder_path}' 不存在。")
            continue

        # 创建新的文件夹用于存储下采样后的图像
        # output_folder = f"{folder_path}_downsampled"
        output_folder = os.path.join('/mnt/data/cyf20/data/UHD-LL_down4', folder_path.split('/')[-2], folder_path.split('/')[-1])
        os.makedirs(output_folder, exist_ok=True)

        # 遍历文件夹中的图片文件
        for filename in os.listdir(folder_path):
            if filename.endswith('.JPG'):
                image_path = os.path.join(folder_path, filename)
                output_path = os.path.join(output_folder, filename)

                # 读取图像
                img = cv2.imread(image_path)

                # 检查是否成功读取图像
                if img is not None:
                    # 进行四倍下采样
                    downsampled_img = cv2.resize(img, None, fx=0.25, fy=0.25, interpolation=cv2.INTER_AREA)
                    
                    # 保存下采样后的图像
                    cv2.imwrite(output_path, downsampled_img)
                    print(f"已处理并保存: {output_path}")
                else:
                    print(f"无法读取图像: {image_path}")

if __name__ == "__main__":
    # 文件夹列表示例
    folders = [
        "/mnt/data/cyf20/data/UHD-LL/testing_set/gt",
        "/mnt/data/cyf20/data/UHD-LL/testing_set/input",
        '/mnt/data/cyf20/data/UHD-LL/training_set/gt',
        '/mnt/data/cyf20/data/UHD-LL/training_set/input'
    ]

    # 调用函数进行处理
    downsample_images(folders)
